package app.netant.bt.btrabbit;

import java.util.HashMap;
import java.util.Map;

import org.jsoup.select.Elements;

import app.netant.NetAntBase;
import app.netant.bt.MargnetDetail;
import app.netant.bt.MargnetEntity;
/**
 * www.btrabbit.la
 * @author JiaJ
 *
 */
public class BtRabbitDetail extends MargnetDetail {

	public BtRabbitDetail(String netSite) {
		super(netSite);
	}
	
	private void analyseName(){
		Elements activePart = document.select("#wrapper #content #wall h2");
		if(activePart.size()>0){
			//System.out.println(activePart.get(0).text());
			this.entity.setMargnetName(activePart.get(0).text());
		}
	}
	
	private void analyseTags(){
		Elements activePart = document.select("#wrapper #content #wall .fileDetail table.detail-table tbody");
		Map<String, String> tagsMap = new HashMap<String, String>();
		if(activePart.size()>0){
			
			Elements tagNames = activePart.get(0).select("tr th");
			Elements tagValues = activePart.get(0).select("tr td");
			
			for(int i=0;i<tagNames.size();i++){
				tagsMap.put(tagNames.get(i).text().trim(), tagValues.get(i).text().trim());
			}
			
			//System.out.println(tagsMap.get("Create Time"));
			//System.out.println(tagsMap.get("File Size"));
			this.entity.setMargnetDate(tagsMap.get("Create Time"));
			this.entity.setMargnetSize(tagsMap.get("File Size"));
			
		}
	}
	
	private void analyseMargnet(){
		Elements activePart = document.select("#wrapper #content #wall .fileDetail .detail-panel .panel-body a");
		if(activePart.size()>0){
			//System.out.println(activePart.get(0).text());
			this.entity.setMargnetURL(activePart.get(0).text());
		}
	}
	
	private void analyseFileList(){
		Elements activePart = document.select("#wrapper #content #wall .fileDetail .detail-panel .panel-body ol li");
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<activePart.size();i++){
			sb.append(activePart.get(0).text()+"\n");
		}
		this.entity.setMargnetFile(sb.toString());
	}
	
	public void analyse(){
		this.analyseName();
		this.analyseTags();
		this.analyseMargnet();
		this.analyseFileList();
	}
	
	public static void main(String[] args) {
		BtRabbitDetail detail = new BtRabbitDetail("https://www.btrabbit.la/wiki/b03cc651b013f22498bfa9d35b693db8cacc42ee.html");
		detail.analyseName();
		detail.analyseTags();
		detail.analyseMargnet();
		detail.analyseFileList();
	}
}
